package cn.chenhuanming.shh.administer.repository;

import cn.chenhuanming.shh.administer.entity.jpa.House;
import cn.chenhuanming.shh.administer.entity.jpa.UserBroker;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Repository;

/**
* Generated by Spring Data Generator on 25/07/2017
*/
@Repository
public interface HouseRepository extends JpaRepository<House, String>, JpaSpecificationExecutor<House>{

    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','SHOP_BROKER','ROLE_SHOP_MANAGER')")
    @Override
    House save(House house);

    @PreAuthorize("hasAnyRole('ROLE_ADMINISTER','SHOP_BROKER','ROLE_SHOP_MANAGER')")
    @Override
    void delete(@Param("id") String id);

    @PreAuthorize("hasRole('SHOP_BROKER')")
    Page<House> findByIsQualityOrIsRecommended(
            @Param("isQuality") String isQuality, @Param("isRecommend")String isRecommend, Pageable pageable);

    @PreAuthorize("hasRole('SHOP_BROKER')")
    @Query(value = "SELECT h FROM House h where h.userBroker.username = ?#{principal.username}")
    Page<House> findBrokerHouse(Pageable pageable);

    @PreAuthorize("hasRole('ROLE_SHOP_MANAGER')")
    @Query(value = "SELECT h FROM House h where h.userBroker.userSystem.username = ?#{principal.username}")
    Page<House> findManagerHouse(Pageable pageable);

    Page<House> findAllByTypeAndUserBroker(
            @Param("type") String type, @Param("userBroker") UserBroker userBroker, Pageable pageable
    );

    Page<House> findAllByTypeAndUserBrokerAndIsRecommended(
            @Param("type") String type, @Param("userBroker") UserBroker userBroker,
            @Param("isRecommended") String isRecommended, Pageable pageable
    );
}
